<template>
    <div class="login_container">
        <div class="login_box">
            <div class="avatar_box">
                <img src="../assets/images/preview.png" alt="">
            </div>

            <el-form ref="formRef" :rules="rules" class="login_form" :model="form">

                <!--用户名-->
                <el-form-item prop="username">
                    <el-input v-model="form.username" prefix-icon="iconfont icon-yonghu"></el-input>
                </el-form-item>

                <!--密码-->
                <el-form-item  prop="password">
                    <el-input type="password" v-model="form.password" prefix-icon="iconfont icon-mima"></el-input>
                </el-form-item>

                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetLogin">重置</el-button>
                </el-form-item>

            </el-form>

        </div>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                //登录表单的数据绑定对象
                form: {
                    username: 'admin',
                    password: '123456'
                },
                //表单的验证规则
                rules: {
                    username: [
                        { required: true, message: '请输入活动名称', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                        ],
                    password: [
                        {required: true, message: '请输入活动名称', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                        ]
                }
            }
        },
        methods : {
            resetLogin(){
                //重置
                this.$refs.formRef.resetFields();
            },
            login(){
                this.$refs.formRef.validate(async (valid) => {
                    if (!valid) return;
                    const {data} =await this.$http.post("login",this.form);
                    console.log(data);
                    if (data.meta.status !== 200){
                        this.$message({
                            type: "error",
                            center: true,
                            message: "登录失败"
                        })
                    }else {
                        this.$message.success("登录成功");
                        setTimeout(() => {
                            window.sessionStorage.setItem("token",data.data.token);
                            //页面跳转
                            this.$router.push("/home")
                        },1000);
                    }
                });
            }

        }
    }
</script>

<style scoped lang="less">

    .login_container {
        background-color: skyblue;
        height: 100%;
    }

    .login_box {
        width: 450px;
        height: 300px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);

        .avatar_box {
            height: 130px;
            width: 130px;
            border: 1px solid #eee;
            border-radius: 50%;
            padding: 10px;
            box-shadow: 0 0 10px #ddd;
            position: absolute;
            left: 50%;
            /*移动自己的位置 这里是向左，向上移动*/
            transform: translate(-50%, -50%);
            background-color: #fff;

            img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background-color: #eee;
            }
        }
        .btns{
            display: flex;
            justify-content: center;
        }

        .login_form{
            position: absolute;
            bottom: 0;
            width: 100%;
            padding: 0 10px;
            box-sizing: border-box;
        }
    }



</style>
